#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <asl.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "FWCommon.h"
#import "FWApplication.h"
#import "FWDetailListEntry.h"
#import "FWDetailListSection.h"
#import "FWPlugin.h"

#import "FWMessageView.h"

#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)

static NSString* const kProductVersion = @STR(APP_VERSION);


@implementation FWMessageView


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management


// ******************************************************************************************
#pragma mark -
#pragma mark Handling user input

- (BOOL)doneClicked:(FWApplication*)application sections:(NSArray*)sections
{
    return YES;
}

- (BOOL)buttonClicked:(FWApplication *)application sections:(NSArray *)sections button:(FWDetailListEntry*)button
{
    FWPlugin* plugin = application.fwPlugin;
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",
                                       @"TrikieD@gmail.com",
                                       [[NSString stringWithFormat:
                                         @"Pocket Informant plug-in problem report - version %@",
                                         kProductVersion]
                                        stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
                                       [[NSString stringWithFormat:@"Error: %@\nInfo: %@\nDetails: %@",
                                         plugin.messageBoxLine1,
                                         plugin.messageBoxLine2,
                                         plugin.messageBoxDetail] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
    
    [plugin.liPlugin performSelector:@selector(launchURL:) withObject:url afterDelay:1.0];

    return YES;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Standard debug info

-(NSArray*)console
{
    NSMutableArray *consoleLog = [NSMutableArray array];
    
    aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
    
    aslmsg query = asl_new(ASL_TYPE_QUERY);
    asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
    aslresponse response = asl_search(client, query);
    
    asl_free(query);
    
    aslmsg message;
    while((message = aslresponse_next(response)))
    {
        const char *msg = asl_get(message, ASL_KEY_MSG);
        if (msg)
            [consoleLog addObject:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]];
    }
    
    aslresponse_free(response);
    asl_close(client);
    
    return consoleLog;
}

- (NSArray*)dataForDetailListPage:(NSObject *)page key:(NSObject*)key application:(FWApplication *)application
{  
    NSMutableArray* messages = [NSMutableArray arrayWithCapacity:2];
    
    [messages addObject:[[[[FWDetailListEntry entryWithTitle:application.fwPlugin.messageBoxLine1 value:@"Mail"]
                           withSize:kFWDetailListViewLabelSize]
                          withNoBold]
                         withButtonType:nil]];
    
    if (application.fwPlugin.messageBoxLine2)
        [messages addObject:[[[FWDetailListEntry entryWithTitle:application.fwPlugin.messageBoxLine2]
                              withSize:kFWDetailListViewLabelSize]
                             withNoBold]];
    
    NSArray* info = [NSArray arrayWithObjects:
                     [[[FWDetailListEntry entryWithTitle:application.fwPlugin.messageBoxDetail]
                       withSize:kFWDetailListViewSystemFontSize]
                      withNoBold],
                     nil];
    
    return [NSArray arrayWithObjects:
            [FWDetailListSection sectionForElements:messages],
            [FWDetailListSection sectionForElements:info],
            nil];
}

- (NSString*)captionForListPage:(NSObject *)page forKey:(NSObject *)key displayDepth:(NSInteger)depth application:(FWApplication *)application
{
    return nil;
}

- (NSString*)listPageTextForKey:(NSObject*)key currentPage:(NSObject*)current displayDepth:(NSInteger)depth application:(FWApplication*)application
{
    return nil;
}

- (NSArray*)detailSectionsForListPage:(NSObject *)page key:(NSObject *)key application:(FWApplication *)application
{
    return nil;
}

@end
